/*
 * This class is responsible for creating all the other classes and controlling/ running them in a 
 * orderly fashion. It is also responsible for creating the single SPS object which would be used
 * across the entire program.
 * 
 * 
 * =========== IMPORTANT (VITAL) : It is essential that the key for the resources are generated in a numeric order starting from 0.
 * =========== This is to ensure that the power feed reader can properly read the power consumption for all the resources.
 */

public class Controller 
{
	protected volatile Boolean running_jobFeeder = null;
	protected volatile Boolean running_jobHandler = null;
	protected volatile Boolean running_serverCluster = null;
	protected volatile Boolean running_powerFeedReader = null;
	protected volatile Boolean running_ApplicationLogger = null;
	
	protected volatile SystemPowerState sps = null;
	protected volatile IServerClusterUsageData sC_UsageData = null;
	protected volatile IServerCluster serverCluster = null;
	protected volatile ApplicationPowerMonitor monitor = null;
	
	private IJobFeeder jobFeeder = null;
	private IJobHandler jobHandler = null;
	private IPowerFeedReader powerFeedReader = null;
	private IApplicationLogger logger = null;
	
	public Controller()
	{
		running_jobFeeder = new Boolean(true);
		running_jobHandler = new Boolean(true);
		running_serverCluster = new Boolean(true);
		running_powerFeedReader = new Boolean(true);
		running_ApplicationLogger = new Boolean(true);
		
		sps = new SystemPowerState();
		sC_UsageData = new ServerClusterUsageData();
		serverCluster = new ServerCluster(running_serverCluster);
		
		jobFeeder = new JobFeeder(running_jobFeeder);
		jobHandler = new JobHandler2(running_jobHandler,sC_UsageData,serverCluster);
		powerFeedReader = new PowerFeedReader(sps,running_powerFeedReader,serverCluster,sC_UsageData);
		monitor = new ApplicationPowerMonitor(sps,jobFeeder,serverCluster,sC_UsageData);
		logger = new ApplicationLogger(serverCluster,sC_UsageData,running_ApplicationLogger,monitor);
	}
	
	public static void main(String args[])
	{
		Controller cntrl = new Controller();
		
		cntrl.jobFeeder.assignJobHandlerObject(cntrl.jobHandler);		
		cntrl.monitor.startWindow();
		
		new Thread(cntrl.jobFeeder).start();
		new Thread(cntrl.jobHandler).start();
		new Thread(cntrl.powerFeedReader).start();
		new Thread(cntrl.logger).start();
	}
	
	public void stopByBoolean()
	{
		running_jobFeeder = new Boolean(false);
		running_jobHandler = new Boolean(false);
		running_serverCluster = new Boolean(false);
	}
}
